package com.sparrows.pirates.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class NavalBattleMap {
	private int battleType, mapWidth, mapHeight, tileSize;
	private Texture textureSheet;
	private TextureRegion water, sand;
	
	private int[][] map;
	private List<Rectangle> boundingBoxes = new ArrayList<Rectangle>();
	private List<Rectangle> bufferBoundingBoxes = new ArrayList<Rectangle>();

	public NavalBattleMap(int batType, int mw, int mh, int ts, Texture texture){		
		//Type of terrain this takes place around.
		//(0)Mostly water, (1)mostly land,(2)a lot of rocks, or (3)mixed.
		battleType = batType;
		
		mapWidth = mw;
		mapHeight = mw;
		tileSize = ts;
		
		mapWidth = 32;
		mapHeight = 32;
		tileSize = 64;
		
		//Get textures
		textureSheet = texture;
		water = new TextureRegion(textureSheet, 0, 0, tileSize, tileSize);
		sand = new TextureRegion(textureSheet, tileSize, 0, tileSize, tileSize);
	}
	
	public void draw(SpriteBatch batch){
		for (int i = 0; i < mapWidth; i++){
			for (int j = 0; j < mapHeight; j++){
				if (map[i][j] == 2){
					
				} else if (map[i][j] == 1){
					batch.setColor(0.2f, 0.5f, 1f, 1f);
					batch.draw(sand, i*tileSize, j*tileSize);
					batch.setColor(Color.WHITE);
					batch.draw(sand, i*tileSize, j*tileSize, tileSize/2, tileSize/2, sand.getRegionWidth(), sand.getRegionHeight(), 0.6f, 0.6f, 0);
				} else {
					batch.draw(water, i*tileSize, j*tileSize);
				}
			}
		}
	}
	
	public void create(){
		map = new int[mapWidth][mapHeight];
		
		for (int i = 0; i < mapWidth; i++){
			for (int j = 0; j < mapHeight; j++){
				//Form a box of water around the player to ensure proper movement
				if ( i >= 14 && i <= 18 && j >= 14 && j <= 18) {
					map[i][j] = 0;
				} else {
					Random rand = new Random();
					map[i][j] = rand.nextInt(99);
				}
			}
		}
		//Optimize map values to tile types
		clean();
		createBoundingBoxes();
	}
	
	public void clean(){
		//Optimize map for land battles
				if (battleType == 1){

				}
				//Optimize map for rock battles
				else if (battleType == 2){

				} 
				//Optimize map for mixed battles
				else if (battleType == 3){

				}
				//Optimize/default to water battles
				else{
					for (int i = 0; i < mapWidth; i++){
						for (int j = 0; j < mapHeight; j++){
							//2% Rock
							if (map[i][j] >= 98){
								map[i][j] = 2;
							//4% Land
							} else if (map[i][j] >= 94 ){
								map[i][j] = 1;
							//95% water
							} else{
								map[i][j] = 0;
							}
						}
					}
				}
	}
	
	public void createBoundingBoxes(){
		for (int i = 0; i < mapWidth; i++){
			for (int j = 0; j < mapHeight; j++){
				if (map[i][j] == 1 || map[i][j] == 2){
					boundingBoxes.add(new Rectangle(i*tileSize+(tileSize*0.2f), j*tileSize+(tileSize*0.2f), 0.6f*tileSize, 0.6f*tileSize));
					bufferBoundingBoxes.add(new Rectangle(i*tileSize, j*tileSize, tileSize, tileSize));
				}
			}
		}
	}
	
	public List<Rectangle> getBoundingBoxes(){
		return this.boundingBoxes;
	}
	public List<Rectangle> getBufferZone(){
		return this.bufferBoundingBoxes;
	}
}
